package com.sophialex.health.record.views.query;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

import com.sophia.pojo.AbstractPage;
import com.sophialex.health.common.platform.SuperContext;
import com.sophialex.health.common.platform.pojo.Geography;
import com.sophialex.health.person.vo.HrPerson;
import com.sophialex.health.record.Messages;
import com.sophialex.health.record.common.util.DataSource;
import com.sophialex.health.record.common.util.EventType;
import com.sophialex.health.record.common.util.HealthRecordListenerRegister;
import com.sophialex.health.record.common.utils.CommonUtils;
import com.sophialex.health.record.common.validate.TextVerifyListener;
import com.sophialex.health.record.perspective.PersonRecordPerspective;
import com.sophialex.health.record.ui.query.HrPersonQueryUI;
import com.sophialex.health.record.views.account.PersonAccountView;
import com.sophialex.health.system.frame.dialog.DialogManager;
import com.sophialex.health.system.frame.util.ViewUitl;

public class HrPersonQueryView extends HrPersonQueryUI {
	public static final String ID = "com.sophialex.health.record.views.query.HrPersonQueryView"; 

	private List<HrPerson> personList;
	private HrPerson hrPerson = new HrPerson();


	@Override
	protected void addListener() {


		firstButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doSetData(hrPerson, 1);
				// doAfterPage();
			}
		});
		preButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doSetData(hrPerson,Integer.valueOf(curPageLabel.getText()) - 1);
				// doAfterPage();
			}
		});
		nextButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doSetData(hrPerson,Integer.valueOf(curPageLabel.getText()) + 1);
				// doAfterPage();
			}
		});
		lastButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doSetData(hrPerson, Integer.valueOf(sumPageLabel.getText()));
				// doAfterPage();
			}
		});
	
		nCountText.addKeyListener(new KeyListener() {

			public void keyReleased(KeyEvent e) {}

			public void keyPressed(KeyEvent e) {
				if (e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR) {
					doSetData(hrPerson,Integer.valueOf(1));
				}
			}
		});
		nPageText.addKeyListener(new KeyListener() {

			public void keyReleased(KeyEvent e) {
				// TODO Auto-generated method stub
				if (e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR) {
					if (nPageText.getText() == null
							|| nPageText.getText().equals("")) { //$NON-NLS-1$
						DialogManager.invokeWarnDlg(Messages.getString("Common.11")); //$NON-NLS-1$
						return;
					}
					if (Integer.valueOf(nPageText.getText()) > Integer
							.valueOf(sumPageLabel.getText())
							|| Integer.valueOf(nPageText.getText()) < 1) {
						DialogManager.invokeWarnDlg(Messages.getString("Common.11")); //$NON-NLS-1$
						return;
					}

					doSetData(hrPerson, Integer.valueOf(nPageText.getText()));
				}
			}

			public void keyPressed(KeyEvent e) {
				// TODO Auto-generated method stub

			}
		});

		txt_sage.addVerifyListener(new TextVerifyListener(1));
		txt_eage.addVerifyListener(new TextVerifyListener(1));
		
		
	}

	@Override
	protected void init() {
		doSetAllData(hrPerson);

	}
    public void doSetAllData(HrPerson queryPerson){
    	doSetData(queryPerson,1);
    }
    
    public void doSetData(HrPerson queryPerson,int page){
    	AbstractPage abstractPage = null;
    	int pageSize = nCountText.getText() == null || nCountText.getText().equals("") ? 5000 : Integer .valueOf(nCountText.getText());
    	abstractPage = DataSource.hrPersonService.queryPageHrPersonList(hrPerson, page,pageSize);
    	personList = abstractPage.getPageList()==null?new ArrayList():abstractPage.getPageList();
    	showTable.setInput(personList);
		sumCountLabel.setText(String.valueOf(abstractPage.getTotalCount()));
		curCountLabel.setText(String.valueOf(personList.size()));
		sumPageLabel.setText(String.valueOf(abstractPage.getPageCount()));
		curPageLabel.setText(String.valueOf(abstractPage.getCurrentPage()));
		doAfterPage();
    	showTable.setSelection(null,false);
    	showTable.redraw();
    }
    
	public void doClear() {
		showTable.setInput(null);
		sumCountLabel.setText(""); //$NON-NLS-1$
		curCountLabel.setText(""); //$NON-NLS-1$
		sumPageLabel.setText(""); //$NON-NLS-1$
		curPageLabel.setText(""); //$NON-NLS-1$

		pdTimeText.setText(""); //$NON-NLS-1$
	}

	public void doAfterPage() {
		if (sumPageLabel.getText().equals("1")) { //$NON-NLS-1$
			firstButton.setEnabled(false);
			preButton.setEnabled(false);
			nextButton.setEnabled(false);
			lastButton.setEnabled(false);
			return;
		}
		if (curPageLabel.getText().equals("1")) { //$NON-NLS-1$
			firstButton.setEnabled(false);
			preButton.setEnabled(false);
			nextButton.setEnabled(true);
			lastButton.setEnabled(true);
		} else if (curPageLabel.getText().equals(sumPageLabel.getText())) {
			firstButton.setEnabled(true);
			preButton.setEnabled(true);
			nextButton.setEnabled(false);
			lastButton.setEnabled(false);
		} else {
			firstButton.setEnabled(true);
			preButton.setEnabled(true);
			nextButton.setEnabled(true);
			lastButton.setEnabled(true);
		}
	}


	public String notNullString(String temp) {
		return temp == null ? "" : temp; //$NON-NLS-1$
	}

	@Override
	protected void doExit() {
		ViewUitl.hideView(this.ID);
	}

	@Override
	protected void doOpenRecord() {
		
		if(showTable.getSelection().size()>0){
			HrPerson selectPerson = (HrPerson) showTable.getSelection().get(0);
			SuperContext.currentPerson = selectPerson;
			HealthRecordListenerRegister.getInstance().postEvent(selectPerson,EventType.REFRESH);
			ViewUitl.openPersperctive(PersonRecordPerspective.ID);		
			
		}else{
			DialogManager.invokeWarnDlg(Messages.getString("FamilyUI_INFO.10")); 
		}
		
	}
    
	protected void doAccount() {
		
		if(showTable.getSelection().size()>0){
			HrPerson selectPerson = (HrPerson) showTable.getSelection().get(0);
			SuperContext.currentPerson = selectPerson;
			getViewSite().getPage().hideView(this);
			PersonAccountView personAccountView = ViewUitl.showView(PersonAccountView.class);
			personAccountView.refresh();
			
		}else{
			DialogManager.invokeWarnDlg(Messages.getString("FamilyUI_INFO.10")); 
		}
		
	}
	
	@Override
	protected void doQuery() {
		hrPerson = new HrPerson();
		hrPerson.setName(text_name.getText());
		hrPerson.setIdno(txt_idno.getText());
		Long sex = CommonUtils.getComboValue(combo_sex).longValue();
		if(sex.longValue() != -1){
			hrPerson.setSex(CommonUtils.getComboValue(combo_sex));
		}
		Long zrys = CommonUtils.getComboValue(combo_zrys).longValue();
		Long jdr = CommonUtils.getComboValue(combo_jdr).longValue();
		hrPerson.setAdminUserId(zrys);
		hrPerson.setArchiveUserId(jdr);
		hrPerson.setArchiveSerial(txt_dabh.getText());
		if(txt_sage.getText() != null && !"".equals(txt_sage.getText())){
			hrPerson.setStartAge(Long.valueOf(txt_sage.getText()));
		}
		if(txt_eage.getText() != null && !"".equals(txt_eage.getText())){
			hrPerson.setEndAge(Long.valueOf(txt_eage.getText()));
		}
		Geography  geography =  (Geography) combo_jwh.getTreeItemData();
		if(geography != null && !"请选择居委会".equals(geography.getfTreemc())){
			hrPerson.setGeographyName(geography.getfTreemc());
		}
		doSetData(hrPerson,1);
	}


}
